<?php

namespace Kateba\GOOBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

/**
 * FundingAgency controller.
 *
 */
class FundingAgencyController extends Controller {

    /**
     * this action will display the funding agencies page
     * @author Mahmoud
     * @param integer $page the page number
     */
    public function indexAction($page = 1) {
        //the results per page number
        $itemsPerPage = 9;
        //the number of funding
        $count = 0;
        //the last page number used for pagination
        $lastPageNumber = 0;
        //get the table data
        $data = $this->getDoctrine()->getRepository('KatebaGOOBundle:FundingAgency')->getFundingAgencies($page, $itemsPerPage);
        //get the objects
        $entities = $data['entities'];
        //get the count of total results
        $count = $data['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        return $this->render('KatebaGOOBundle:FundingAgency:index.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber
                ));
    }

    /**
     * Finds and displays a FundingAgency entity.
     * @author Mahmoud
     */
    public function showAction($id) {
        //try to find the requested funding agency
        $entity = $this->getDoctrine()->getEntityManager()->getRepository('KatebaGOOBundle:FundingAgency')->find($id);
        //check if we found it
        if (!$entity) {
            //not found go to 404 page
            throw $this->createNotFoundException('Unable to find FundingAgency entity.');
        }
        return $this->render('KatebaGOOBundle:FundingAgency:show.html.twig', array(
                    'entity' => $entity,
                ));
    }

}
